from baseCommand import *
import textwrap

class _If(baseCommand):
	ifstr = textwrap.dedent("""\
			If (%s)
			{
			%s
			}
			""")
	elsestr = textwrap.dedent("""\
			Else
			{
			%s
			}
			""")

	def __init__(self, expr, iftrue="Return", iffalse=""):
		self.name = "If"
		this.expr = expr
		this.iftrue = iftrue
		this.iffalse = iffalse

	def If(self, expr, iftrue="Return", iffalse="", append=True):
		print "If", "expr", expr, "iftrue", iftrue, "iffalse", iffalse, "append", append

		if append:
			self.commands.append(self._If_tostr(expr, iftrue, iffalse))
		else:
			return _If(expr, iftrue, iffalse)

	def _If_tostr(self, expr, iftrue="Return", iffalse=""):
		ret = ""
		ret = ret + self.ifstr % (expr, iftrue)
		if ( iffalse != "" ):
			ret = ret + self.elsestr % (iffalse)
		return ret

	def __str__(self):
		return self._If_tostr(self.expr, self.iftrue, self.iffalse)